package com.darren.euc.entity;

import java.io.Serializable;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonFormat;

@Entity
@Table(name = "euc_client")
public class Client implements Serializable {

	private static final long serialVersionUID = 2861478466077717714L;

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	@Column(name = "client_type")
	private String clientType;

	@Column(name = "member_name")
	private String memberName;

	@Column(name = "member_address")
	private String memberAddress;

	@Column(name = "member_id_card")
	private String memberIdCard;

	/**
	 * 拟在空间开展业务
	 */
	@Column(name = "member_business")
	private String memberBusiness;
	
	/**
	 * 公司名称
	 */
	@Column(name = "company_name")
	private String companyName;

	@Column(name = "company_logo")
	private String logoUrl;

	/**
	 * 统一信用代码
	 */
	@Column(name = "company_credit_code")
	private String companyCreditCode;

	/**
	 * 公司住所
	 */
	@Column(name = "company_address")
	private String companyAddress;
	
	/**
	 * 法定代表人
	 */
	@Column(name = "company_legal_person")
	private String companyLegalPerson;
	
	/**
	 * 注册资本
	 */
	@Column(name = "company_registered_capital")
	private String companyRegisteredCapital;	
	
	/**
	 * 实收资本
	 */
	@Column(name = "company_real_capital")
	private String companyRealCapital;
		
	/**
	 * 企业类型
	 */
	@Column(name = "company_enterprise_type")
	private String companyEnterpriseType;
	
	/**
	 * 成立日期
	 */
	@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
	@Column(name = "company_create_date")
	private Date companyCreateDate;
	
	/**
	 * 主营业务
	 */
	@Column(name = "company_business")
	private String companyBusiness;	
	
	/**
	 * 经营范围
	 */
	@Column(name = "company_business_scope")
	private String companyBusinessScope;	
	
	@Transient
	private String clientStatus="";
	
	
	/**
	 * 入驻日期
	 */
	@Transient
	private String enterTime="";
	
	/**
	 * 离场日期
	 */
	@Transient
	private String leaveTime="";	
	
	/**
	 * 入驻房间名
	 */
	@Transient
	private String roomNames="";	
	
	
	@Transient
	private String billStatus="未结清";
	
	/**
	 * 备注
	 */
	@Column(name = "remark")
	private String remark;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn(name = "client_Id")
	private List<ClientExtension> clientExtensionList = new ArrayList<ClientExtension>();
	
	@OneToMany(mappedBy = "client", cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
	@OrderBy(" createTime DESC ")
	private List<Order> orderList = new ArrayList<Order>();


	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	@Column(name = "create_time",nullable=true,columnDefinition="datetime")
	private Date createTime;

	@Column(name = "create_user_id")
	private Integer createUserId;
	
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	@Column(name = "last_modify_time",nullable=true,columnDefinition="datetime")
	private Date lastModifyTime;
	
	@Column(name = "last_modify_user_id")
	private Integer lastModifyUserId;
	
	@Transient
	private Clue clue;

	@Transient
	private BigInteger memberCount;

	@Column(name = "customerType")
	private String  customerType;//所属行业
	@Column(name = "enterprise_authority")
	private String enterpriseAuthority;//资质
	@Column(name = "owner_id_card")
	private String ownerIdCard;//法人身份证
	@Column(name = "personal_id_card")
	private String personalIdCard;//个人身份证
	@Column(name = "enterprise_phone")
	private String enterprisePhone;//企业联系电话
	@Column(name = "main_contacter")
	private String mainContacter;//主要联系人

	public String getEnterprisePhone() {
		return enterprisePhone;
	}

	public void setEnterprisePhone(String enterprisePhone) {
		this.enterprisePhone = enterprisePhone;
	}

	public String getMainContacter() {
		return mainContacter;
	}

	public void setMainContacter(String mainContacter) {
		this.mainContacter = mainContacter;
	}

	public String getCustomerType() {
		return customerType;
	}

	public void setCustomerType(String customerType) {
		this.customerType = customerType;
	}

	public String getEnterpriseAuthority() {
		return enterpriseAuthority;
	}

	public void setEnterpriseAuthority(String enterpriseAuthority) {
		this.enterpriseAuthority = enterpriseAuthority;
	}

	public String getOwnerIdCard() {
		return ownerIdCard;
	}

	public void setOwnerIdCard(String ownerIdCard) {
		this.ownerIdCard = ownerIdCard;
	}

	public String getPersonalIdCard() {
		return personalIdCard;
	}

	public void setPersonalIdCard(String personalIdCard) {
		this.personalIdCard = personalIdCard;
	}

	public String getLogoUrl() {
		return logoUrl;
	}

	public void setLogoUrl(String logoUrl) {
		this.logoUrl = logoUrl;
	}

	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getClientType() {
		return clientType;
	}


	public void setClientType(String clientType) {
		this.clientType = clientType;
	}


	public String getMemberName() {
		return memberName;
	}


	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}


	public String getMemberAddress() {
		return memberAddress;
	}


	public void setMemberAddress(String memberAddress) {
		this.memberAddress = memberAddress;
	}


	public String getMemberIdCard() {
		return memberIdCard;
	}


	public void setMemberIdCard(String memberIdCard) {
		this.memberIdCard = memberIdCard;
	}


	public String getMemberBusiness() {
		return memberBusiness;
	}


	public void setMemberBusiness(String memberBusiness) {
		this.memberBusiness = memberBusiness;
	}


	public String getCompanyName() {
		return companyName;
	}


	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}


	public String getCompanyCreditCode() {
		return companyCreditCode;
	}


	public void setCompanyCreditCode(String companyCreditCode) {
		this.companyCreditCode = companyCreditCode;
	}


	public String getCompanyAddress() {
		return companyAddress;
	}


	public void setCompanyAddress(String companyAddress) {
		this.companyAddress = companyAddress;
	}


	public String getCompanyLegalPerson() {
		return companyLegalPerson;
	}


	public void setCompanyLegalPerson(String companyLegalPerson) {
		this.companyLegalPerson = companyLegalPerson;
	}


	public String getCompanyRegisteredCapital() {
		return companyRegisteredCapital;
	}


	public void setCompanyRegisteredCapital(String companyRegisteredCapital) {
		this.companyRegisteredCapital = companyRegisteredCapital;
	}


	public String getCompanyRealCapital() {
		return companyRealCapital;
	}


	public void setCompanyRealCapital(String companyRealCapital) {
		this.companyRealCapital = companyRealCapital;
	}


	public String getCompanyEnterpriseType() {
		return companyEnterpriseType;
	}


	public void setCompanyEnterpriseType(String companyEnterpriseType) {
		this.companyEnterpriseType = companyEnterpriseType;
	}


	public Date getCompanyCreateDate() {
		return companyCreateDate;
	}


	public void setCompanyCreateDate(Date companyCreateDate) {
		this.companyCreateDate = companyCreateDate;
	}


	public String getCompanyBusiness() {
		return companyBusiness;
	}


	public void setCompanyBusiness(String companyBusiness) {
		this.companyBusiness = companyBusiness;
	}


	public String getCompanyBusinessScope() {
		return companyBusinessScope;
	}


	public void setCompanyBusinessScope(String companyBusinessScope) {
		this.companyBusinessScope = companyBusinessScope;
	}


	public String getRemark() {
		return remark;
	}


	public void setRemark(String remark) {
		this.remark = remark;
	}


	public List<ClientExtension> getClientExtensionList() {
		return clientExtensionList;
	}


	public void setClientExtensionList(List<ClientExtension> clientExtensionList) {
		this.clientExtensionList = clientExtensionList;
	}
	
	public List<Order> getOrderList() {
		return orderList;
	}


	public void setOrderList(List<Order> orderList) {
		this.orderList = orderList;
	}


	public Date getCreateTime() {
		return createTime;
	}


	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}


	public Integer getCreateUserId() {
		return createUserId;
	}


	public void setCreateUserId(Integer createUserId) {
		this.createUserId = createUserId;
	}


	public Date getLastModifyTime() {
		return lastModifyTime;
	}


	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}


	public Integer getLastModifyUserId() {
		return lastModifyUserId;
	}


	public void setLastModifyUserId(Integer lastModifyUserId) {
		this.lastModifyUserId = lastModifyUserId;
	}


	
	
	public String getClientStatus() {		
		if(orderList != null && orderList.size() > 0){	
			clientStatus = "已签约";
			Date minDate = null;
			Date maxDate = null;
			for(Order order:orderList){
				for(Contract contract:order.getContractList()){
					for(ContractProduct contractProduct:contract.getContractProductList()){
						if(minDate == null || (contractProduct.getRentStart() != null && minDate.getTime() > contractProduct.getRentStart().getTime())){
							minDate = contractProduct.getRentStart();
						}
						if(maxDate == null || (contractProduct.getRentEnd() != null && maxDate.getTime() < contractProduct.getRentEnd().getTime())){
							maxDate = contractProduct.getRentEnd();
						}
					}
				}
			}
			if(minDate != null || maxDate != null){
				Date currentDate = new Date();
				if(minDate.getTime() < currentDate.getTime() && maxDate.getTime() > currentDate.getTime()){
					clientStatus = "已入驻";
				}
				if(maxDate.getTime() < currentDate.getTime()){
					clientStatus = "已离场";
				}
			}
		}else{
			clientStatus = "未签约";
		}		
		return clientStatus;
	}


	public void setClientStatus(String clientStatus) {
		this.clientStatus = clientStatus;
	}

	

	public String getBillStatus() {
		return billStatus == null?"":billStatus;
	}


	public void setBillStatus(String billStatus) {
		this.billStatus = billStatus;
	}


	public Clue getClue() {
		return clue;
	}


	public void setClue(Clue clue) {
		this.clue = clue;
	}
	
	


	public BigInteger getMemberCount() {
		return memberCount;
	}


	public void setMemberCount(BigInteger memberCount) {
		this.memberCount = memberCount;
	}


	public String getEnterTime() {
		enterTime = "";
		if(orderList != null && orderList.size() > 0){	
			Date minDate = null;
			for(Order order:orderList){
				for(Contract contract:order.getContractList()){
					for(ContractProduct contractProduct:contract.getContractProductList()){
						if(minDate == null || (contractProduct.getRentStart() != null && minDate.getTime() > contractProduct.getRentStart().getTime())){
							minDate = contractProduct.getRentStart();
						}
					}
				}
			}
			if(minDate != null){
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				enterTime = format.format(minDate);
			}
		}		
		return enterTime;
	}


	public void setEnterTime(String enterTime) {
		this.enterTime = enterTime;
	}


	public String getLeaveTime() {
		leaveTime = "";
		if(orderList != null && orderList.size() > 0){	
			Date maxDate = null;
			for(Order order:orderList){
				for(Contract contract:order.getContractList()){
					for(ContractProduct contractProduct:contract.getContractProductList()){
						if(maxDate == null || (contractProduct.getRentEnd() != null && maxDate.getTime() < contractProduct.getRentEnd().getTime())){
							maxDate = contractProduct.getRentEnd();
						}
					}
				}
			}
			if(maxDate != null){
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				leaveTime = format.format(maxDate);
			}
		}	
		return leaveTime;
	}


	public void setLeaveTime(String leaveTime) {
		this.leaveTime = leaveTime;
	}


	public String getRoomNames() {
		roomNames = "";
		if(orderList != null && orderList.size() > 0){	
			for(Order order:orderList){
				for(Contract contract:order.getContractList()){
					for(ContractProduct contractProduct:contract.getContractProductList()){
						if("".equals(roomNames)){
							roomNames = contractProduct.getRoomName();
						}else{
							roomNames += "," + contractProduct.getRoomName();
						}
					}
				}
			}
			
		}		
		return roomNames;
	}


	public void setRoomNames(String roomNames) {
		this.roomNames = roomNames;
	}
	
	
	
	
}
